// pages/articleDetail/articleDetail.js
const articleList = require("../../models/articleList");
const articleCollect = require("../../models/articleCollect");
Page({
  data: {
    value:[],
    state:true
  },
  onLoad: async function (options) {

    // 音乐播放
    this.audioCtx = wx.createAudioContext('myAudio')
    this.audioCtx.play();//默认播放

    //获取详情页数据
    var res = await articleList();
    var data = res.data.res.filter(res=>{
      return res._id == options.id
    })
    this.setData({
      value:data[0]
    })
  },

  //收藏按钮 
  async collect(event){
    if(event.currentTarget.dataset.collected){
      var state = false
    }else{
      var state = true
    }
    await articleCollect(event.currentTarget.dataset.id,state);//请求收藏路由，操作数据库
    var value = this.data.value;
    value.collected = state
    this.setData({
      value,
    })
  },

  // 改变音乐播放状态
  change(){
    if(this.data.state){
      this.audioCtx.pause()
    }else{
      this.audioCtx.play()
    }
    this.setData({
      state:!this.data.state
    })
  }
})